<template>
  <div class="comdiv">

    <happy-scroll  color="#ccc" size="5" class="layout-scrollbar__wrap" :scroll-top="scrolltop" resize hide-horizontal>
      <div class="con" ref="bgImage">
      <!-- search1 -->
      <search-temp
        :listQuery="listQuery.searchText"
        @doSearch="handleFilter"
        @handleCreate="handleCreate"
        :placeholder="placeholder"
      ></search-temp>
      <!--table-->
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" label="序号" width="100">
          <template slot-scope="scope"><span>{{scope.row.px}}</span></template>
        </el-table-column>
        <el-table-column align="center" label="id" width="100">
          <template slot-scope="scope"><span>{{scope.row._id}}</span></template>
        </el-table-column>

        <el-table-column align="center" label="区域名" prop="areaName">
          <template slot-scope="scope"><span>{{scope.row.areaName}}</span></template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createdTime">
          <template slot-scope="scope"><span>{{scope.row.createdTime}}</span></template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <!--<el-button type="success" plain @click="handleUpdate(scope.row)" size="small">-->
            <el-button type="primary" plain @click="handleUpdate(scope.row)" size="small">修改</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--add-->
      <el-dialog :title="dialogStatus=='create'?'添加区域':'修改区域'" :visible.sync="dialogFormVisible">
        <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="100px"
                 style='width: 450px; margin-left:50px;'>
          <el-form-item label="区域名" prop="areaName">
            <el-input v-model="temp.areaName"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button v-if="dialogStatus=='create'" type="primary" @click="createData" id="cfmbtm1">添加</el-button>
          <el-button v-else type="primary" @click="updateData" id="cfmbtm2">修改</el-button>
        </div>
      </el-dialog>
      <!--edit-->
      <!--分页组件-->
<!--      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size"
                  @pagination="getList"/>-->

    </div>

    </happy-scroll>


  </div>
</template>

<script>

  /* eslint-disable */
  import {
    fetchGet,
    createOne,
    updateOne,
    deleteOnePut
  } from '@/api/common'
  import {disableBtn,ERRS, SUCS} from '@/utils'
  // import Pagination from '@/components/Pagination'
  import searchTemp from '@/components/viewComponents/search2'
  // import $ from 'jquery'

  export default {
    name: 'areaList',
    // search2 引入 and search3 声明
    components: {searchTemp},

    data() {
      return {
        url:'auth/areas',
        list: null,
        // total: 110,
        // px: null, //最大序号
        listLoading: true,
        placeholder: '区域', //search4
        listQuery: {
          page: 1,
          size: 10, //每页几条
          number: 0, //当前页为第1页,从0计算
          searchText: undefined,
        },
        //弹出层
        dialogFormVisible: false,
        dialogStatus: '',
        tempDetail: {},
        temp: {},   //保存添加,编辑后提交的信息
        rules: {
          areaName: [
            {required: true, message: '请输入区域名', trigger: 'blur'}
          ],
        },
      }
    },
    mounted() {
      // console.log(this.$store.state.user.company)
      this.getList();
    },
    methods: {

      ////search5
      handleFilter(val) {
        this.listQuery.searchText = val;
        this.getList()
      },
      getList() {
        this.listLoading = true;
        fetchGet(this.url+`/all?size=${this.listQuery.size}`).then(res => {
          console.log(res);
          if (res.code == 0) {
            let dat = res.data
            // this.listQuery.size = dat.size ? dat.size : //每页几条
            // this.listQuery.page = dat.page ? dat.page : //当前页为第1页
            this.list = dat;
            let insIndex = this.listQuery.size * (this.listQuery.page - 1);
            this.list = this.list.map((v, index) => {
              this.$set(v, 'px', insIndex + index + 1);
              // this.$set(v, 'roles',insIndex+index+1);
              this.px = insIndex + index + 1; //最大序号
              return v
            });

            this.$nextTick(function () {
              console.log('v-for渲染已经完成')

            })
          } else {
            ERRS(res.message)
          }
          this.listLoading = false
        })
      },
      handleCreate() {
        this.temp={
          areaName:'',
        };
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //添加提交
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm1');
            createOne(this.url, this.temp).then(res => {
              if (res.code === 0) {
                SUCS('添加成功!')
                this.getList();
              } else {
                ERRS(res.message)
              }
              this.dialogFormVisible = false
            })
          }
        })
      },
      //详情修改
      handleUpdate(row) {
        this.temp = {
          areaName:row.areaName,
          id:row._id
        }
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate();
        })
      },
      //修改提交
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm2');
            // delete tempData.createdTime;
            updateOne(this.url, this.temp).then(res => {
              if (res.code === 0) {
                SUCS('修改成功!')
                this.getList();
              } else {
                ERRS(res.message)
              }
              this.dialogFormVisible = false
            })
          }
        })
      },
      //删除
      handleDelete(row) {
        console.log(row);
        this.$msgbox({
          // title: '删除',
          message: '确定要删除吗?',
          showCancelButton: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(action => {
          deleteOnePut(`auth/areas/freeze`,row._id).then(res => {
            if (res.code === 0) {
              this.getList();
              SUCS('删除成功!')
            } else {
              ERRS(res.message)
            }
            this.dialogFormVisible = false
          })
        });
      },

    }
  }
</script>

<style  rel="stylesheet/scss" lang="scss">
  @import './comp/index.scss';
</style>
